// 顶部导入新增
import { Game, canvas, ctx } from './game-state.js';  
import { gameLoop } from './game-loop.js';
import { controlPlayer, drawPlayer } from './player.js';

// 删除原有 canvas 声明，保留设置画布尺寸的代码
import { Enemy } from './enemy.js';
//const ctx = canvas.getContext('2d');
// 设置画布尺寸
canvas.width = 800;
canvas.height = 600;



// 启动游戏 (修改启动方式)
requestAnimationFrame(gameLoop); // 现在使用导入的 gameLoop

// 事件监听
window.addEventListener('keydown', (e) => {
    Game.keys[e.key] = true;
});

window.addEventListener('keyup', (e) => {
    Game.keys[e.key] = false;
});

// 射击功能
export function shootBullet() {
    // 检查是否按住 q 键并且按下空格键
    if (Game.keys['q'] && Game.keys[' ']) {
        const bulletCount = 5; // 发射子弹的数量
        const angleSpread = Math.PI / 8; // 子弹散射的角度范围
        const baseDirection = Game.player.direction;

        for (let i = 0; i < bulletCount; i++) {
            const angleOffset = angleSpread * (i - (bulletCount - 1) / 2);
            const direction = {
                x: Math.cos(baseDirection + angleOffset),
                y: Math.sin(baseDirection + angleOffset)
            };

            Game.bullets.push({
                x: Game.player.x + Game.player.width / 2,
                y: Game.player.y + Game.player.height / 2,
                direction: direction,
                speed: 10
            });
        }
    } else if (Game.keys[' ']) {
        // 单颗子弹发射
        Game.bullets.push({
            x: Game.player.x + Game.player.width / 2,
            y: Game.player.y + Game.player.height / 2,
            direction: { x: Math.cos(Game.player.direction), y: Math.sin(Game.player.direction) },
            speed: 10
        });
    }
}

